package com.example.message.mapper;

import com.example.message.entity.MessageReceive;

import org.apache.ibatis.annotations.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 通知接收Mapper接口
 */
@Mapper
public interface MessageReceiveMapper{

	@Select({ "SELECT * FROM t_message_receive WHERE del_flag = 0 AND receive_id = #{receiveId}" })
	MessageReceive findById(Long receiveId);

	@Select({ "SELECT * FROM t_message_receive WHERE del_flag = 0 AND message_id = #{messageId} AND receiver_id = #{receiverId}" })
	MessageReceive findByMessageIdAndReceiverId(Long messageId, Long receiverId);

	@Update({ "<script>", "update t_message_receive", "<trim prefix=\"SET\" suffixOverrides=\",\">",
			"<if test=\"messageId != null\">message_id = #{messageId},</if>",
			"<if test=\"receiverId != null\">receiver_id = #{receiverId},</if>",
			"<if test=\"receiverName != null\">receiver_name = #{receiverName},</if>",
			"<if test=\"deptId != null\">dept_id = #{deptId},</if>",
			"<if test=\"deptName != null\">dept_name = #{deptName},</if>",
			"<if test=\"readStatus != null\">read_status = #{readStatus},</if>",
			"<if test=\"readTime != null\">read_time = #{readTime},</if>",
			"<if test=\"handleStatus != null\">handle_status = #{handleStatus},</if>",
			"<if test=\"handleRemark != null\">handle_remark = #{handleRemark},</if>",
			"<if test=\"handleTime != null\">handle_time = #{handleTime},</if>",
			"update_time = NOW(),",
			"</trim>", "where receive_id = #{receiveId}", "</script>" })
	public int updateById(MessageReceive messageReceive);

	/**
	 * 根据通知ID查询接收列表
	 * 
	 * @param messageId 通知ID
	 * @return 接收记录列表
	 */
	@Select({ "SELECT * FROM t_message_receive WHERE message_id = #{messageId}", "AND del_flag = 0",
			"ORDER BY create_time DESC" })
	List<MessageReceive> selectByNoticeId(@Param("messageId") Long messageId);

	/**
	 * 根据接收人ID查询接收列表
	 * 
	 * @param receiverId 接收人ID
	 * @return 接收记录列表
	 */
	@Select({ "SELECT nr.*", "FROM t_message_receive nr", "INNER JOIN t_message n ON n.message_id = nr.message_id",
			"WHERE nr.receiver_id = #{receiverId}", "AND n.del_flag = 0", "AND n.status = 1", "AND nr.del_flag = 0",
			"ORDER BY n.send_time DESC" })
	List<MessageReceive> selectByReceiverId(@Param("receiverId") Long receiverId);

	/**
	 * 批量插入接收记录
	 * 
	 * @param receiveList 接收记录列表
	 * @return 插入的记录数
	 */
	@Insert({ "<script>", "INSERT INTO t_message_receive (",
			"message_id, receiver_id, receiver_name, dept_id, dept_name,",
			"read_status, handle_status, create_time, update_time, del_flag", ") VALUES ",
			"<foreach collection='list' item='item' separator=','>", "(",
			"#{item.messageId}, #{item.receiverId}, #{item.receiverName},",
			"#{item.deptId}, #{item.deptName}, #{item.readStatus},",
			"#{item.handleStatus}, #{item.createTime}, #{item.updateTime}, 0", ")", "</foreach>", "</script>" })
	int batchInsert(@Param("list") List<MessageReceive> receiveList);

	/**
	 * 标记为已读
	 * 
	 * @param receiveIds 接收记录ID列表
	 * @param readTime   阅读时间
	 * @return 更新的记录数
	 */
	@Update({ "<script>", "UPDATE t_message_receive", "SET read_status = 1,", "read_time = #{readTime},",
			"update_time = #{readTime}", "WHERE receive_id IN",
			"<foreach collection='receiveIds' item='id' open='(' separator=',' close=')'>", "#{id}", "</foreach>",
			"AND del_flag = 0", "</script>" })
	int markAsRead(@Param("receiveIds") List<Long> receiveIds, @Param("readTime") LocalDateTime readTime);

	/**
	 * 标记为已处理
	 * 
	 * @param receiveIds   接收记录ID列表
	 * @param handleStatus 处理状态
	 * @param handleRemark 处理备注
	 * @param handleTime   处理时间
	 * @return 更新的记录数
	 */
	@Update({ "<script>", "UPDATE t_message_receive", "SET handle_status = #{handleStatus},",
			"handle_remark = #{handleRemark},", "handle_time = #{handleTime},", "update_time = #{handleTime}",
			"WHERE receive_id IN", "<foreach collection='receiveIds' item='id' open='(' separator=',' close=')'>",
			"#{id}", "</foreach>", "AND del_flag = 0", "</script>" })
	int markAsHandled(@Param("receiveIds") List<Long> receiveIds, @Param("handleStatus") Integer handleStatus,
			@Param("handleRemark") String handleRemark, @Param("handleTime") LocalDateTime handleTime);

	@Select({ "SELECT * FROM t_message_receive", "WHERE del_flag = 0 AND message_id = #{messageId}" })
	List<MessageReceive> findListByMessageId(Long messageId);

	@Select({ "SELECT count(1) FROM t_message_receive ",
			"WHERE del_flag = 0 AND receiver_id = #{receiverId} AND read_status = #{readStatus}" })
	Long findTotalByReceiverIdAndReadStatus(Long receiverId, int readStatus);

	/**
	 * 根据ID列表批量查询接收记录
	 * 
	 * @param receiveIds 接收记录ID列表
	 * @return 接收记录列表
	 */
	@Select({ "<script>", 
		"SELECT * FROM t_message_receive", 
		"WHERE receive_id IN",
		"<foreach collection='receiveIds' item='id' open='(' separator=',' close=')'>", 
		"#{id}", 
		"</foreach>",
		"AND del_flag = 0", 
		"ORDER BY create_time DESC", 
		"</script>" })
	List<MessageReceive> selectBatchIds(@Param("receiveIds") List<Long> receiveIds);
	
	/**
	 * 查询指定时间之后的新通知接收记录
	 * 
	 * @param receiverId 接收人ID
	 * @param lastCheckTime 上次检查时间（毫秒时间戳）
	 * @return 新通知接收记录列表
	 */
	@Select({ "SELECT * FROM t_message_receive", 
		"WHERE receiver_id = #{receiverId}",
		"AND del_flag = 0",
		"AND create_time > FROM_UNIXTIME(#{lastCheckTime}/1000)",
		"ORDER BY create_time DESC" })
	List<MessageReceive> findNewByReceiverIdAfterTime(@Param("receiverId") Long receiverId, @Param("lastCheckTime") Long lastCheckTime);

}